package com.behome.netty.netty_D.bean;

import cn.hutool.json.JSONObject;
import com.behome.netty.common.ByteConventerKit;
import com.behome.netty.common.enums.TerminalWorkStatus;
import com.behome.netty.netty_D.enums.TerminalDAnnotation;
import com.behome.netty.netty_D.enums.TerminalDType;
import org.apache.commons.lang3.StringUtils;

import java.nio.ByteBuffer;


@TerminalDAnnotation(
        type = TerminalDType.HEART_BEAT
)
public class TerminalDHeartBeat extends BusinessCmdBase {
    private TerminalWorkStatus workStatus;
    private byte firstRegisterFlag;
    private String physicalAddress;

    public TerminalDHeartBeat() {
        this.workStatus = TerminalWorkStatus.FREE;
        this.firstRegisterFlag = 1;
    }

    public TerminalWorkStatus getWorkStatus() {
        return this.workStatus;
    }

    public void setWorkStatus(TerminalWorkStatus workStatus) {
        this.workStatus = workStatus;
    }

    public byte getFirstRegisterFlag() {
        return this.firstRegisterFlag;
    }

    public void setFirstRegisterFlag(byte firstRegisterFlag) {
        this.firstRegisterFlag = firstRegisterFlag;
    }

    public String getPhysicalAddress() {
        return this.physicalAddress;
    }

    public void setPhysicalAddress(String physicalAddress) {
        this.physicalAddress = physicalAddress;
    }

    public byte[] serialize() {
        if (!this.validateBeforeSerialize()) {
            return null;
        } else {
            byte[] macdata = ByteConventerKit.macStrToBytes(this.physicalAddress);
            int length = 3 + macdata.length;
            ByteBuffer byteBuffer = ByteBuffer.allocate(length);
            byteBuffer.put(this.workStatus.getValue())
                    .put(this.firstRegisterFlag)
                    .put((byte) macdata.length)
                    .put(macdata);
            return byteBuffer.array();
        }
    }

    public BusinessCmdBase deserialize(byte[] businessData) {
        if (businessData != null && businessData.length > 3) {
            ByteBuffer byteBuffer = ByteBuffer.wrap(businessData);
            this.setWorkStatus(TerminalWorkStatus.getEnumByValue(byteBuffer.get()));
            this.setFirstRegisterFlag(byteBuffer.get());
            byte tmpPhysicalAddressLength = byteBuffer.get();
            if (tmpPhysicalAddressLength > 0) {
                byte[] address = new byte[tmpPhysicalAddressLength];
                byteBuffer.get(address);
                this.setPhysicalAddress(ByteConventerKit.bytesToMacStr(address, false));
            }
        }

        return this;
    }

    public boolean validateBeforeSerialize() {
        if (StringUtils.isBlank(this.physicalAddress)) {
            throw new IllegalArgumentException("physicalAddress不能为空.");
        } else {
            return true;
        }
    }

    public String toCHJsonString() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.putOnce("工作状态", this.workStatus.getName());
        jsonObject.putOnce("注册标识", (this.firstRegisterFlag == 1 ? "首次注册" : "非首次注册"));
        jsonObject.putOnce("物理地址", this.physicalAddress);
        return jsonObject.toString();
    }
}
